﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Tormenta.Utils.Animation
{
    public class IntervalAnimation: Timer
    {
        #region .ctro

        public IntervalAnimation(double interval, List<Texture2D> lstSprites)
            : base(interval)
        {
            this.CurrentIndex = 0;
            this.IsPaused = false;
            this.Sprites = lstSprites;
            this.OnOverflow += new EventHandler<TimerEventArgs>(SimpleAnimation_OnOverflow);            
        }

        #endregion

        #region Properties

        public List<Texture2D> Sprites { get; set; }

        public bool IsPaused { get; set; }

        public int CurrentIndex { get; private set; }

        public Vector2 Position { get; set; }

        public Texture2D CurrentSprite
        {
            get
            {
                return this.Sprites[this.CurrentIndex];
            }
        }

        #endregion

        #region Methods

        public override void Update(double elapsedTime)
        {
            if (!this.IsPaused)
                base.Update(elapsedTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.CurrentSprite, this.Position, Util.TRANSPARENT_COLOR);
        }

        public void Play()
        {
            this.IsPaused = false;
        }

        public void Stop()
        {
            this.IsPaused = true;
        }

        #endregion

        #region Events

        void SimpleAnimation_OnOverflow(object sender, TimerEventArgs e)
        {
            this.CurrentIndex++;
            if (this.CurrentIndex >= this.Sprites.Count)
                this.CurrentIndex = 0;
        }

        #endregion

    }
}
